package com.md24housecall.fibromyalgiadiagnostic;


import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class QuestionOneFragment extends Fragment {
    final static String ARG_POSITION = "position";
    int mCurrentPosition = -1;
    boolean [] checkedCheckBoxs = new boolean [20];
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) {

        // If activity recreated (such as from screen rotate), restore
        // the previous article selection set by onSaveInstanceState().
        // This is primarily necessary when in the two-pane layout.
        if (savedInstanceState != null) {
            mCurrentPosition = savedInstanceState.getInt(ARG_POSITION);
        }

        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.question_one_fragment, container, false);
        
        checkedCheckBoxs = ((MainActivity)getActivity()).getQuestionOneCheckedCheckBoxs();
        
        LinearLayout layout = (LinearLayout)view.findViewById(R.id.checkbox_body);
        for(int i = 0; i < layout.getChildCount(); i++){
        	CheckBox checkBox = (CheckBox) layout.getChildAt(i);
        	checkBox.setChecked(checkedCheckBoxs[i]);
        	if(checkedCheckBoxs[i]){
        		checkBox.setTypeface(null, Typeface.BOLD);
        		checkBox.setTextColor(getResources().getColor(R.color.checked_text));
        		checkBox.setShadowLayer(2, 2, 2, R.color.shadow_text);
        	}else{
        		checkBox.setTypeface(null, Typeface.NORMAL);
            	checkBox.setTextColor(getResources().getColor(R.color.unchecked_text));
            	checkBox.setShadowLayer(0, 0, 0, R.color.shadow_text);
        	}
        }
        
        Button button = (Button) view.findViewById(R.id.next_button);
        button.setOnClickListener((OnClickListener) new ButtonEvent());
        
        Button viewImage = (Button) view.findViewById(R.id.view_image_button);
        viewImage.setOnClickListener((OnClickListener) new ViewImage());
		return view;
    }
    
    private class ViewImage implements OnClickListener{

    	@SuppressWarnings("static-access")
		@Override
        public void onClick(View v) {
    		Activity activity = getActivity();
            AlertDialog.Builder imageDialog = new AlertDialog.Builder(activity);
            LayoutInflater inflater = (LayoutInflater) activity.getSystemService(activity.LAYOUT_INFLATER_SERVICE);

            View layout = inflater.inflate(R.layout.view_image,
                    (ViewGroup) getView().findViewById(R.id.layout_root));
            ImageView image = (ImageView) layout.findViewById(R.id.fullimage);
            image.setImageDrawable(getView().getResources().getDrawable(R.drawable.body1));
            imageDialog.setView(layout);
            imageDialog.setPositiveButton("Close", new DialogInterface.OnClickListener(){

                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }

            });


            imageDialog.create();
            imageDialog.show();  

        }

    }


    private class ButtonEvent implements OnClickListener{

    	@Override
        public void onClick(View v) {
    		QuestionTwoFragment newFragment = new QuestionTwoFragment();
            Bundle args = new Bundle();
            //args.putInt(NoticeFragment.ARG_POSITION, position);
            newFragment.setArguments(args);
            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            transaction.setCustomAnimations(R.anim.in_from_right, R.anim.out_to_left, R.anim.in_from_left, R.anim.out_to_right);

            transaction.replace(R.id.fragment_container, newFragment);
            transaction.addToBackStack(null).commit();

        }

    }

    
    @Override
    public void onStart() {
        super.onStart();

        // During startup, check if there are arguments passed to the fragment.
        // onStart is a good place to do this because the layout has already been
        // applied to the fragment at this point so we can safely call the method
        // below that sets the article text.
        Bundle args = getArguments();
        if (args != null) {
            // Set article based on argument passed in
           // updateArticleView(args.getInt(ARG_POSITION));
        } else if (mCurrentPosition != -1) {
            // Set article based on saved instance state defined during onCreateView
            //updateArticleView(mCurrentPosition);
        }
    }

   
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        // Save the current article selection in case we need to recreate the fragment
        outState.putInt(ARG_POSITION, mCurrentPosition);
    }
}